﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JSC.Portal.Services;
using System.Web.Mvc;
using JSC.Portal.Entities;
using NHibernate;
using JSC.Data.NHibernate;
using System.IO;
using System.Web;

namespace JSC.Portal.Web.Controllers
{
    [Authorize(Roles = "Administrator,SiteAdministrator")]
    [HandleError]
    public class TemplatesController : ServicedController<ITemplateService>
    {
        public ActionResult List()
        {
            IList<Template> templates = Service.GetAll();
            return View(templates);
        }

        public ActionResult Edit(int id)
        {
            Template t = Service.GetById(id, false);
            return View(t);
        }

        public ActionResult SaveOrUpdate(int id, string name, string description, string path, string controlname, string css)
        {
            using (ITransaction trans = NHibernateSession.Current.BeginTransaction())
            {
                Template t = Service.GetById(id, false, true);
                t.Name = name;
                t.Description = description;
                t.Path = path;
                t.ControlName = controlname;
                t.CSS = css;
                t = Service.SaveOrUpdate(t);
                trans.Commit();
                return RedirectToAction("Edit", new { id = t.Id });
            }
        }

        public ActionResult Create(int id)
        {
            Template t = new Template();
            return View("Edit", t);
        }

        public ActionResult Delete(int id)
        {
            using (ITransaction trans = NHibernateSession.Current.BeginTransaction())
            {
                Template t = Service.GetById(id, false);
                Service.Delete(t);
                trans.Commit();
                return RedirectToAction("List");
            }
        }

        public ActionResult GetTemplateSelect(int id, string path)
        {
            Template t = Service.GetById(id, false, true);
            string templatePath = VirtualPathUtility.ToAbsolute(path);
            templatePath = Server.MapPath(templatePath);
            string cssPath = Path.Combine(templatePath, @"CSS\");
            string[] templates = new string[] { };
            string[] csss = new string[] { };

            if (Directory.Exists(templatePath))
            {
                templates = Directory.GetFiles(templatePath, "*.ascx");
                csss = Directory.Exists(cssPath) ? Directory.GetFiles(cssPath, "*.css") : new string[0];

                for (int i = 0; i < templates.Length; i++)
                {
                    FileInfo fi = new FileInfo(templates[i]);
                    templates[i] = fi.Name;
                }
                for (int i = 0; i < csss.Length; i++)
                {
                    FileInfo fi = new FileInfo(csss[i]);
                    csss[i] = fi.Name;
                }
            }
            SelectList controlnames = new SelectList(templates, t.ControlName);
            SelectList stylesheets = new SelectList(csss, t.CSS);
            ViewData["ControlNames"] = controlnames;
            ViewData["CSSs"] = stylesheets;
            return PartialView("TemplateSelectEditControl");
        }
    }
}
